package edu.hnu.cloudflight.android;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.baidu.push.example.LoginActivity;

import edu.hnu.cloudflight.android.domain.ServerRequest;
import edu.hnu.cloudflight.android.supply.DateBtnOnclick;
import edu.hnu.cloudflight.android.util.DateUtil;
import edu.hnu.cloudflight.android.util.HttpUtil;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FlightReserveActivity extends Fragment {

	private static final String TAG = "FlightReserveActivity";

	private EditText flightNum;
	private TextView showDate;
	private Button setDateBtn;
	private Button reserveBtn;
	
	private SubmitReserve reserveTask;
	private ProgressDialog pd;
	private String token;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_flightreserve, container, false);

		return v;
	}

	@Override
	public void onResume() {
		flightNum = (EditText) getView().findViewById(R.id.flightreserve_edit_flightnumber);
		setDateBtn = (Button) getView().findViewById(R.id.flighrserver_btn_id_setdate);
		showDate = (TextView) getView().findViewById(R.id.flighrserver_text_id_showdate);
		reserveBtn = (Button) getView().findViewById(R.id.flightreserve_btn_id_submitreserve);
		
		String date = DateUtil.getNowDateToString(DateUtil.sdf1);
		showDate.setText(date);
		
		setDateBtn.setOnClickListener(new DateBtnOnclick(showDate, getActivity()));

		reserveBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				token = LoginActivity.getAccessToken(getActivity());
				if (token == null || "".equals(token)) {
					new AlertDialog.Builder(getActivity()).setTitle(R.string.flightreserve_diag_notlogin)
							.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									Intent intent = new Intent(getActivity(), LoginActivity.class);
									startActivity(intent);
								}
							}).show();
					return;
				}
				pd = new ProgressDialog(getActivity());
				// pd.setTitle(R.string.flightnumberresult_diag_pleasewait);
				pd.setMessage(getResources().getString(R.string.flightreserve_pd_text_issubmit));
				pd.setIndeterminate(true);
				pd.setCancelable(true);
				pd.show();
				if (reserveTask != null) {
					reserveTask.cancel(true);
				}
				reserveTask = new SubmitReserve();
				reserveTask.execute("");
			}
		});
		super.onResume();
	}

	private class SubmitReserve extends AsyncTask<String, String, ServerRequest> {

		@Override
		protected ServerRequest doInBackground(String... params) {
			if (flightNum == null) {
				return null;
			}

			String date = showDate.getText().toString();
			String flightNumText = flightNum.getText().toString();
			Log.i(TAG, flightNumText);

			if (flightNumText != null && !"".equals(flightNumText)) {
				Map<String, String> param = new HashMap<String, String>();
				param.put("token", token);
				param.put("flightnum", flightNumText);
				param.put("type", "flightwarning");
				param.put("date", date);
				try {
					Log.i(TAG, token);
					String result = HttpUtil.getStringByGet(MyApplication.submitReserveUrl, param);
					Log.i(TAG, result);
					ServerRequest sr = JSON.parseObject(result, ServerRequest.class);
					return sr;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(ServerRequest sr) {
			// TODO Auto-generated method stub
			if (sr != null && "200".equals(sr.resultcode)) {
				if (pd != null) {
					pd.cancel();
				}
				new AlertDialog.Builder(getActivity()).setTitle(R.string.flightreserve_diag_submitsuccess)
						.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								
							}
						}).show();

			} else {
				if (pd != null) {
					pd.cancel();
				}
				new AlertDialog.Builder(getActivity()).setTitle(R.string.flightreserve_diag_submitsuccess).setMessage(sr.reason)
						.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								
							}
						}).show();
			}

		}

	}

}
